package com.example.dllo.exe.giftspeak.songnvpiao;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.widget.ListView;

import com.example.dllo.exe.R;
import com.example.dllo.exe.bean.SongNvPiaoBean;
import com.example.dllo.exe.fengzhuang.NetTool;
import com.example.dllo.exe.giftspeak.jingxuan.MyListView;
import com.example.dllo.exe.main.BaseFragment;

/**
 * code is far away from bug with the animal protecting
 * <p/>
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃永无BUG！  凯哥 祝你一路顺风
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 */
public class SongNvPiaoTopFragment extends BaseFragment {
    private String url;//网址
    private MyListView giftspeaklistView_top;
    private SongNvPiaoAdapter songNvPiaoAdapter;


    public static SongNvPiaoTopFragment getInstance(String url){
        SongNvPiaoTopFragment tempFragment = new SongNvPiaoTopFragment();
        Bundle bundle = new Bundle();
        bundle.putString("url",url);
        tempFragment.setArguments(bundle);
        return tempFragment;
    }



    @Override
    protected int setLayout() {
        return  R.layout.songnvyoufragmentlayout;
    }

    @Override
    protected void initView() {
        giftspeaklistView_top = bindView(R.id.listview_songnvPiao_top);
    }

    @Override
    protected void initData() {

        Bundle arguments = getArguments();
        url = arguments.getString("url");

        mNetTool.getNetData(url, SongNvPiaoBean.class, new NetTool.NetListener<SongNvPiaoBean>() {
            @Override
            public void onSuccess(final SongNvPiaoBean songNvPiaoBean) {
                songNvPiaoAdapter = new SongNvPiaoAdapter(getContext());
                songNvPiaoAdapter.setOnclick(new SongNvPiaoAdapter.ItemOnclick() {
                    @Override
                    public void onItemClick(int pos) {


                        Intent intent=new Intent(getContext(),SongNvPiaoSecondTopAty.class);
                        intent.putExtra("key",songNvPiaoBean.getData().getItems().get(pos).getUrl());

                        startActivity(intent);

                    }
                });
                giftspeaklistView_top.setAdapter(songNvPiaoAdapter);

                songNvPiaoAdapter.setSongNvPiaoBean(songNvPiaoBean);


            }

            @Override
            public void onError(String errorMsg) {

            }
        });
        giftspeaklistView_top.setonRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new AsyncTask<Void, Void, Void>() {

                    @Override
                    protected Void doInBackground(Void... params) {

                        try {
                            Thread.sleep(1500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        return null;
                    }

                    @Override
                    protected void onPostExecute(Void aVoid) {
                        songNvPiaoAdapter.notifyDataSetChanged();
                        giftspeaklistView_top.onRefreshComplete();
                    }
                }.execute(null, null, null);
            }
        });

    }
}
